﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;
using System.Security.Cryptography;

public partial class Manager_User_AddUser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminUserID"] == null)
        {
            Response.Write("<script language=javascript>alert('Time is out.');window.open('../Default.aspx','_top');</script>");
            return;
        }
        Ajax.Utility.RegisterTypeForAjax(typeof(Manager_User_AddUser));

        if (!IsPostBack)
        {
            BindLevel();
            BindData();
        }

        EnableControl();
    }
    private void EnableControl()
    {
        bool flag =false;
        if (Session["IsAdmin"] == "S")
            flag = true;
        txtRePassword.Enabled = flag;
        txtPassword.Enabled = flag;
        txtEmail.Enabled = flag;
        txtLogin.Enabled = flag;
        ddlAuthority.Enabled = flag;
        txtMenu.Enabled = flag;
        ddlSex.Enabled = flag;
        btnAdd.Visible = flag;
  
    }
    private void BindData()
    {
        if (Request.QueryString["UserID"] != "" && Request.QueryString["UserID"] != null)
        {
            clsWebShop cls = new clsWebShop();        
            DataTable dt = cls.Get_UserByUserIDNoneProc(Request.QueryString["UserID"]).Tables[0];
            if (dt.Rows.Count > 0)
            {
                
                hidEmail.Text = "aaa";
                hidUser.Text = "aaa";
                txtMenu.Text = dt.Rows[0]["CusNm"].ToString();
                txtLogin.Text = dt.Rows[0]["UserNm"].ToString();
                txtEmail.Text = dt.Rows[0]["Email"].ToString();
                txtPassword.Text = dt.Rows[0]["Password"].ToString();
                txtRePassword.Text = dt.Rows[0]["Password"].ToString();
                ddlSex.SelectedItem.Selected = false;
                ddlSex.Items.FindByValue(dt.Rows[0]["sex"].ToString()).Selected = true;
                ddlAuthority.SelectedItem.Selected = false;
                ddlAuthority.Items.FindByValue(dt.Rows[0]["AutID"].ToString()).Selected = true;            
              
            }
        }
    }


    private string QueryStringStr(string str)
    {
        return str + "=" + Request.QueryString[str];

    }
    private void ReturnPage()
    {
        Response.Redirect("User.aspx?CodeID=" + Request.QueryString["CodeID"]+"&" + QueryStringStr("sAutID"));
    }

    private void BindLevel()
    {
        clsWebShop cls = new clsWebShop();
        ddlAuthority.DataSource = cls.Get_AuthorityOderAuthorityNameNoneProc().Tables[0];
        ddlAuthority.DataTextField = "AutNm";
        ddlAuthority.DataValueField = "AutID";
        ddlAuthority.DataBind();
        ListItem item = new ListItem();
        item.Text = "--Chọn quyền hạn--";
        item.Value = "0";
        ddlAuthority.Items.Insert(0, item);
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        PccMsg msg = new PccMsg();
        clsCommon clsCom = new clsCommon();
        RijndaelManaged mobjCryptoService = new RijndaelManaged();
        EncrypKey clsEncryp = new EncrypKey(mobjCryptoService);

        string UserID = "0";
        if (Request.QueryString["UserID"] != "" && Request.QueryString["UserID"] != null)
            UserID = Request.QueryString["UserID"];

        msg.CreateFirstNode("UserID", UserID);
        msg.CreateFirstNode("CusNm", txtMenu.Text);
        msg.CreateFirstNode("Email", txtEmail.Text);
        msg.CreateFirstNode("UserNm", txtLogin.Text);
        msg.CreateFirstNode("Password", clsEncryp.Encrypting((txtPassword.Text), System.Configuration.ConfigurationManager.AppSettings["EncrypKey"]));
        msg.CreateFirstNode("Sex", ddlSex.SelectedValue);
        msg.CreateFirstNode("AutID", ddlAuthority.SelectedValue);
        msg.CreateFirstNode("PlaceID", "0");
        msg.CreateFirstNode("Rate", "0");
        msg.CreateFirstNode("UpdID", Session["AdminUserID"].ToString());

        clsWebShop cls = new clsWebShop();
        cls.Ins_Upd_shr_User(msg.GetXmlStr.ToString());
        ReturnPage();
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        ReturnPage();
    }
    [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.ReadWrite)]
    public string AjaxCheckUser(string value, string ExcUserID)
    {
        string flag = "no";
        clsWebShop cls = new clsWebShop();
        DataTable dt = new DataTable();
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("UserNm", value.Trim());
        msg.CreateFirstNode("ExcUserID", ExcUserID);
        dt = cls.Get_shr_UserByUserNm(msg.GetXmlStr.ToString()).Tables[0];
        if (dt.Rows.Count > 0)
            flag = "yes";
        return flag;
    }
    [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.ReadWrite)]
    public string AjaxCheckEmail(string value,string ExcUserID)
    {
        string flag = "no";
        clsWebShop cls = new clsWebShop();
        DataTable dt = new DataTable();
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("ExcUserID", ExcUserID);
        msg.CreateFirstNode("Email", value.Trim());
        dt = cls.Get_shr_UserByUserEmail(msg.GetXmlStr.ToString()).Tables[0];
        if (dt.Rows.Count > 0)
            flag = "yes";
        return flag;
    }

}
